const {ipcRenderer} = window.top.require('electron');
const {dialog} = window.top.require('electron').remote;
var XLSX = window.top.require('xlsx');

function download(path, fileName) {
    var o = dialog.showSaveDialog({
        title: '保存文件',
        filters: [{
            name: "Spreadsheets",
            extensions: ['xlsx', 'xlsx']
        }],
        defaultPath: fileName
    });
    ipcRenderer.send('download', path + "+" + o);
}

/**
 * 上次文件
 */
function upload() {
    var o = dialog.showOpenDialog({
        title: '选择一个文件',
        filters: [{
            name: "Spreadsheets",
            extensions: "xls|xlsx".split("|")
        }],
        properties: ['openFile']
    });
    if (o.length > 0) return process_wb(XLSX.readFile(o[0]));
}

function process_wb(wb) {
    var o = new Object();

    var sheets = wb.SheetNames;
    for (var i = 0; i < sheets.length; i++) {
        o = XLSX.utils.sheet_to_json(wb.Sheets[sheets[i]], {editable: true});
        break;
    }
    return o;
}

/***
 * 文件导出
 * header 数组,标题
 * data 数组对象
 * fileName 文件名称
 */
function exportData(header, data, fileName) {
    //header ["S","h","e","e_1","t","J","S_1"]
    //data[{"s":"aa"}]
    var ws = XLSX.utils.json_to_sheet(data, {header: header});
    var o = dialog.showSaveDialog({
        title: '保存文件',
        filters: [{
            name: "Spreadsheets",
            extensions: ['xls', 'xlsx']
        }],
        defaultPath: fileName
    });

    var wb = {
        SheetNames: ['mySheet'],
        Sheets: {
            'mySheet': ws
        }
    };

    if (o != null && o != undefined) {
        var layerIndex = layer.open({
            type: 1,
            closeBtn: 0, //不显示关闭按钮
            area: ['300px'],
            content: "<div class='update'>正在导出数据,请等待...</div>"
        });
        XLSX.writeFile(wb, o);
        layer.close(layerIndex);
        layer.msg('数据导出完成!', {time: 2000});
        return true;
    } else {
        return false;
    }
}


const electron = window.top.require('electron');

function getUser(callback) {
    // console.log(ipcRenderer.sendSync('get-user-info', ''));
    var user = ipcRenderer.sendSync('get-user-info', '');
    //callback(user);
    // ipcRenderer.on('user-info', (event, user) => {
    //     callback(user);
    //   });
    setTimeout(function () {
        // console.log('执行结束');
        //let result = 4;
        callback(user);
    }, 100);
}

function getToken(callback) {
    var token = [];
    db.selectBysql("select * from t_company ", [],
        function (d) {
            var company_save = d[0];
            var creditCode = company_save.creditCode;
            console.log(creditCode);
            var password = creditCode.substring(0, 8);
            console.log(password);
            var streetOther = company_save.streetOther;
            //获取社会信用代码
            token[0] = creditCode;
            //获取门店
            if (!isNull(streetOther)) {
                token[3] = streetOther;
            }else{
                token[3] = company_save.enterpriseName;
            }

            var version = fs.readFileSync("./baseinfo.ini").toString();
            var versions = version.split(';');
            $.ajax({
                url: versions[8],
                type: "POST",
                datatype: 'json',
                headers: {
                    "contentType": "application/x-www-form-urlencoded",
                    "appName": "ClouderCorner"
                },
                data: {
                    username: creditCode,
                    password: password
                },
                success: function (data) {
                    console.log(data);
                    if (data.code == 200) {
                        //获取token
                        token[1] = data.TOKENID;
                        //获取上传数据接口
                        token[2] = versions[9];
                        callback(token);
                        console.log(token);
                    } else {
                        callback(201);
                    }
                },
                error: function () {
                    callback(201);
                }
            });
        });
}
